package com.ming.easyDevelop.controller.base;

import com.ming.easyDevelop.utils.CommonResponseCodeEnum;
import com.ming.easyDevelop.entity.vo.ResponseVo;
import com.ming.easyDevelop.exception.BusinessException;
public class BaseController {
    protected static final String STATUS_SUCCESS = "success";
    protected static final String STATUS_ERROR = "error";

    /**
     * 正常返回
     * @param data
     * @return
     * @param <T>
     */
    protected <T> ResponseVo getSuccessResponseVo(T data){
        ResponseVo<T> responseVo = new ResponseVo<>();
        responseVo.setStatus(STATUS_SUCCESS);
        responseVo.setCode(CommonResponseCodeEnum.SUCCESS.getCode());
        responseVo.setData(data);
        responseVo.setMessage(CommonResponseCodeEnum.SUCCESS.getMessage());
        return responseVo;
    }

    /**
     * 業務異常返回
     * @param data
     * @return
     * @param <T>
     */
    protected <T> ResponseVo getBusinessExecptionResponseVo(T data, BusinessException e){
        ResponseVo<T> responseVo = new ResponseVo<>();
        responseVo.setStatus(STATUS_SUCCESS);
        responseVo.setCode(e.getCode());
        responseVo.setData(data);
        responseVo.setMessage(e.getMessage());
        return responseVo;
    }

    /**
     * 服務器異常
     * @param data
     * @return
     * @param <T>
     */
    protected <T> ResponseVo getServerErrorResponseVo(T data){
        ResponseVo<T> responseVo = new ResponseVo<>();
        responseVo.setStatus(STATUS_ERROR);
        responseVo.setCode(CommonResponseCodeEnum.SERVER_ERROR.getCode());
        responseVo.setData(data);
        responseVo.setMessage(CommonResponseCodeEnum.SERVER_ERROR.getMessage());
        return responseVo;
    }
}
